/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Clases.Cliente;
import Clases.Factura;
import Clases.SFacturacion;
import Clases.Servicio;
import Clases.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import Persistencia.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JAVA
 */
@WebServlet(name = "SBaja", urlPatterns = {"/SBaja"})
public class SBaja extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int baja = 0;
        String tipo = request.getParameter("id");
        String codigo = request.getParameter("codigo");

        if (tipo.equals("clienteb")) {
            baja = PersistenciaCliente.bajaCliente(codigo);
            request.setAttribute("insertado", baja);
            Cliente cliente = PersistenciaCliente.obtenerCliente(codigo);
            request.setAttribute("cliente", cliente);
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=Clientes");

            rd.forward(request, response);
        } else if (tipo.equals("usuariob")) {
            baja = PersistenciaUsuario.bajaUsuario(codigo);
            request.setAttribute("insertado", baja);
            Usuario usuario = PersistenciaUsuario.obtenerUsuario(codigo);
            request.setAttribute("usuario", usuario);
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=Usuarios");
            rd.forward(request, response);
        } else if (tipo.equals("BSFacturacion")) {
            
            baja = PersistenciaSFacturacion.eliminarSerie(codigo);
            
            request.setAttribute("insertado", baja);
            
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=SeriesFac");
            rd.forward(request, response);

        } else if (tipo.equals("serviciob")) {
            baja = PersistenciaServicio.eliminarServicio(codigo);
            request.setAttribute("insertado", baja);

            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=Servicios");
            rd.forward(request, response);
        } else if(tipo.equals("TipoIva")){
            
            baja = PersistenciaTiposIva.eliminaTipoIva(PersistenciaTiposIva.obtenterTipoIva(codigo));
            request.setAttribute("insertado", baja);
            
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=TiposIva");
            rd.forward(request, response);
        } else if(tipo.equals("BGasto")){
            
            baja = PersistenciaGasto.eliminarGasto(codigo);
            request.setAttribute("insertado", baja);
            
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=Gastos");
            rd.forward(request, response);
            
        } else if(tipo.equals("BFactura")){
            baja = PersistenciaFactura.borrarFactura(codigo);
            request.setAttribute("insertado", baja);
            
            RequestDispatcher rd = request.getRequestDispatcher("SVer?id=Facturas");
            rd.forward(request, response);            
            
        }
        
            
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SBaja</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SBaja at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(SBaja.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(SBaja.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
